<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="../layui/css/layui.css">
    <script src="../layui/layui.js"></script>
    <script src="../scripts/jquery.min.js"></script>
    <script src="../js/jquery-form.js"></script>

    <style type="text/css">
        .for_img_upload{

        }
    </style>

    <title>试题信息修改</title>
    <style>
        body{padding: 10px;}
        .change_size{
            height: 150px;
        }
        .change_size2{
            height: 100px;
        }
    </style>
</head>

<body>

<input type="hidden" id="flag_manager" value=<%=session.getAttribute("flag_manager")%> />


<input type="hidden" id="flag_edit" value="no" />

<form  class="layui-form layui-form-pane1" id="subform" method="post">

    <input type="hidden" name="question_id" id="question_id" value=${param.question_id} />

    <div class="layui-form-item">
        <label class="layui-form-label">试题来源</label>
        <div class="layui-input-block">
            <input type="text" id="question_ori" name="question_ori"  lay-verify="required|title"  placeholder="请输入来源" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">试题科目</label>
        <div class="layui-input-block" onblur="choose_blur()">
            <select id="question_class" name="question_class" lay-filter="adminRole" >
                <option value="">请选择类型</option>
                <option value="科一">科一</option>
                <option value="科四">科四</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">试题章节</label>
        <div class="layui-input-block">
            <select id="question_chapter" name="question_chapter" lay-filter="adminRole" lay-verify="" lay-search>
            </select>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">答案</label>
        <div class="layui-input-block">
            <input class="for_check" type="checkbox" name="question_answer_a" id="question_answer_a" title="A">
            <input class="for_check" type="checkbox" name="question_answer_b" id="question_answer_b" title="B" >
            <input class="for_check" type="checkbox" name="question_answer_c" id="question_answer_c" title="C">
            <input class="for_check" type="checkbox" name="question_answer_d" id="question_answer_d" title="D">
        </div>
    </div>
    <input type="hidden" name="question_answer" id="question_answer" value="" />

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">题干</label>
        <div class="layui-input-block">
            <textarea id="question_content" name="question_content" placeholder="请输入内容或上传文件（优先选择文件）" class="layui-textarea change_size"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项</label>
        <div class="layui-input-block">
            <textarea id="question_option" name="question_option" placeholder="请输入内容或上传文件（优先选择文件）" class="layui-textarea change_size2"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">试题图片</label>
        <div class="layui-input-block">
            <input type="text" id="question_img" name="question_img"  readonly="readonly" lay-verify="required|title"  placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" id="question_video_input_div" hidden="hidden">
        <label class="layui-form-label">试题动画</label>
        <div class="layui-input-block">
            <input type="text" id="question_video" name="question_video"  readonly="readonly" lay-verify="required|title"  placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div>
        <div style="float: left;">
            <div class="layui-form-item">
                <label class="layui-form-label">上传题干</label>
                <div class="layui-input-block">
                    <input type="file" name="file_content" id="file_content" accept="text/plain" />
                </div>
            </div>
        </div>


        <div style="float: right">
            <div class="layui-form-item">
                <button type="button" class="layui-btn for_img_upload" id="img_update_btn" >
                    <i class="layui-icon">&#xe67c;</i>修改图片
                </button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img for_img_upload" id="img_update" width="110" height="110">
                </div>
            </div>
        </div>

        <div>
            <div class="layui-form-item">
                <label class="layui-form-label">上传选项</label>
                <div class="layui-input-block">
                    <input type="file" name="file_option" id="file_option" accept="text/plain" />
                </div>
            </div>
        </div>

        <div id="question_video_div" hidden="hidden">
            <div class="layui-form-item">
                <label class="layui-form-label">修改动画</label>
                <div class="layui-input-block">
                    <input type="file" name="file_video" id="file_video" accept="video/*" />
                </div>
            </div>
        </div>

    </div>




</form>
<br>
<div style="float: left">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="add_btn" onclick="to_submit()" >立即修改</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</div>


<script>
    //加载layui信息
    var form;
    layui.use('form', function(){
        form = layui.form;
        //layui日期选择
        layui.use('laydate', function(){
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#createTime', //指定元素
                value: new Date()
            });
        });
    });

    var layer;
    layui.use('layer', function(){
        layer = layui.layer;

    })

    window.onload=function (){
        $.ajax({
            url:"${pageContext.request.contextPath}/question/question_table_edit_init.do",
            data:{
                "question_id":$('#question_id').val()
            },
            success:function (data,status){
                if(status=="success"){
                    $("#question_content").val(data.question_content);
                    $("#question_option").val(data.question_option);
                    $("#question_img").val(data.question_img);
                    $("#question_ori").val(data.question_ori);
                    $("#question_video").val(data.question_video);
                    var path="${pageContext.request.contextPath}/img_artificial/"+"question_img/"+data.question_img;
                    var select = 'dd[lay-value=' + data.question_class + ']';
                    var question_answer=data.question_answer;

                    if(question_answer.indexOf("A")!=-1)
                        $("#question_answer_a").prop("checked", true);
                    if(question_answer.indexOf("B")!=-1)
                        $("#question_answer_b").prop("checked", true);
                    if(question_answer.indexOf("C")!=-1)
                        $("#question_answer_c").prop("checked", true);
                    if(question_answer.indexOf("D")!=-1)
                        $("#question_answer_d").prop("checked", true);

                    $('#question_class').siblings("div.layui-form-select").find('dl').find(select).click();
                    $("#img_update").attr("src",path);
                    if(data.question_class=="科一"){
                        createOption(1);
                    }
                    if(data.question_class=="科四"){
                        $("#question_video_input_div").removeAttr("hidden");
                        createOption(4);
                    }
                    var select2='dd[lay-value=' + data.question_chapter + ']';
                    $('#question_chapter').siblings("div.layui-form-select").find('dl').find(select2).click();
                    form.render("checkbox");
                }
            }
        })
    }

    function to_submit(){
        var checkbox=$(".for_check");
        var answer="";
        var question_content=$("#question_content").val();
        var question_option=$("#question_option").val()
        var file_content=$("#file_content").val();
        var file_option=$("#file_option").val();
        var question_class=$("#question_class").val();
        var question_chapter=$("#question_chapter").val();
        var question_ori=$("#question_ori").val();

        if(question_ori==""){
            layer.msg("来源不能为空");
            return ;
        }
        if(question_class==""){
            layer.msg("科目不能为空");
            return ;
        }
        if(question_chapter==""){
            layer.msg("章节不能为空");
            return ;
        }
        if(question_content==""){
            if(file_content==""){
                layer.msg("题干不能为空");
                return ;
            }
        }
        if(question_option==""){
            if(file_option==""){
                layer.msg("选项不能为空");
                return ;
            }
        }
        for(var i=0;i<checkbox.length;i++){
            if(checkbox[i].checked){
                answer+=checkbox[i].title;
            }
        }
        if(answer==""){
            layer.msg("答案不能为空");
            return ;
        }
        $("#question_answer").val(answer);
        if($("#flag_manager").val()!="null"){
            ajaxFile("${pageContext.request.contextPath}/question/question_table_edit.do");
        }else{
            layer.msg("修改失败，您不是管理员");
        }

    }
    function ajaxFile(url){

        $("#subform").ajaxSubmit({
            url:url,
            type:"post",
            enctype:'multipart/form-data',
            success:function(data,status){
                if(status=="success") {
                    $("#flag_edit").val("yes");
                    $("#question_content").val(data.str_content);
                    $("#question_option").val(data.str_option);
                    $("#question_video").val(data.str_video);
                    layer.msg("数据修改成功", {icon: 1});
                }
            }
        })
    }

    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#img_update' //绑定元素
            ,url: '${pageContext.request.contextPath}/question/question_table_edit_upload_img.do' //上传接口
            ,data:{
                "question_id":$("#question_id").val()
            }
            ,accept:"images"
            ,acceptMine:"images/*"
            ,auto:false
            ,bindAction:"#img_update_btn"
            ,choose:function (obj){
                obj.preview(function(index,file,result){
                    $('#img_update').attr('src', result);
                });
            }
            ,done: function(res){
                //上传完毕回调
                if(res.data!="fail") {
                    $("#flag_edit").val("yes");
                    $("#question_img").val(res.data);
                    layer.msg("图片修改成功");
                }else{
                    layer.msg("操作失败，您不是管理员");
                }
            }
            ,error: function(){
                //请求异常回调
                layer.msg("上传失败");
            }
        });
    });

    function choose_blur(){
        // $("#question_chapter").removeAttr('disabled');
        if($("#question_class").val()==""){
            $("#question_chapter").empty();
            var option0=$("<option></option>").val("").text("请先选择科目");
            $("#question_chapter").append(option0);
            $("#question_chapter").attr('disabled','disabled');
            $("#question_video_div").attr("hidden","hidden");
            $("#file_video").val("");
            $("#question_video_input_div").attr("hidden","hidden");
        }else{
            $("#question_chapter").removeAttr('disabled');
            if($("#question_class").val()=="科一"){
                createOption(1);
                $("#question_video_div").attr("hidden","hidden");
                $("#file_video").val("");
                $("#question_video_input_div").attr("hidden","hidden");
            }else{
                createOption(4);
                $("#question_video_div").removeAttr("hidden");
                $("#question_video_input_div").removeAttr("hidden");
            }
        }
        form.render('select');
    }
    function createOption(num){
        $("#question_chapter").empty();
        var option0=$("<option></option>").val("").text("请选择章节");
        $("#question_chapter").append(option0);
        if(parseInt(num)==1){
            var option1=$("<option></option>").val("1、道路交通安全法律、法规和规章").text("1、道路交通安全法律、法规和规章");
            var option2=$("<option></option>").val("2、交通信号").text("2、交通信号");
            var option3=$("<option></option>").val("3、安全行车、文明驾驶基础知识").text("3、安全行车、文明驾驶基础知识");
            var option4=$("<option></option>").val("4、机动车驾驶操作相关基础知识").text("4、机动车驾驶操作相关基础知识");
            var option5=$("<option></option>").val("5、交通事故救护常识").text("5、交通事故救护常识");
            $("#question_chapter").append(option1,option2,option3,option4,option5);
        }else{
            var option1=$("<option></option>").val("1、违法行为综合判断与案例分析").text("1、违法行为综合判断与案例分析");
            var option2=$("<option></option>").val("2、安全行车常识").text("2、安全行车常识");
            var option3=$("<option></option>").val("3、常见交通标志、标线和交通手势辨识").text("3、常见交通标志、标线和交通手势辨识");
            var option4=$("<option></option>").val("4、驾驶职业道德和文明驾驶常识").text("4、驾驶职业道德和文明驾驶常识");
            var option5=$("<option></option>").val("5、恶劣气候和复杂道路条件下驾驶常识").text("5、恶劣气候和复杂道路条件下驾驶常识");
            var option6=$("<option></option>").val("6、紧急情况下避险常识").text("6、紧急情况下避险常识");
            var option7=$("<option></option>").val("7、交通事故救护及常见危化品处置常识").text("7、交通事故救护及常见危化品处置常识");
            $("#question_chapter").append(option1,option2,option3,option4,option5,option6,option7);
        }

    }



</script>

</body>
</html>
